package com.ccjq.shosestore.web.controller.shoppingcart;

import com.ccjq.shosestore.core.utils.UuidUtils;
import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.product.bo.Product;
import com.ccjq.shosestore.product.service.ProductService;
import com.ccjq.shosestore.product.service.impl.ProductServiceImpl;
import com.ccjq.shosestore.shoppingcart.bo.ShopcartBo;
import com.ccjq.shosestore.shoppingcart.service.Impl.ShopcartImpl;
import com.ccjq.shosestore.shoppingcart.service.Shopcart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "shoppingcartServlet")
public class shoppingcartServlet extends HttpServlet {
    private Shopcart sh = new ShopcartImpl();
    private ProductService pr = new ProductServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int a = 0;
        String action = request.getParameter("action");
        User user = (User) request.getSession().getAttribute("user");
        if (action.equals("shopcar")) {
            int quntity = Integer.parseInt(request.getParameter("quantity"));
            String prid = request.getParameter("prid");
            boolean b = sh.selectstock(prid);
            if (user == null) {
                request.getRequestDispatcher("/user?flag=").forward(request, response);
            } else {
                if (b) {
                    String carid = UuidUtils.generateShortUUID("U", 10);
                    System.out.print(carid);
                    Product product = pr.details(prid);
                    sh.insertCar(carid, prid, quntity, user.getUserid());
                    List<ShopcartBo> list = sh.selectCar(user.getUserid());
                    for (ShopcartBo sb1 : list) {
                        a += sb1.getXiaoji();
                    }
                    request.setAttribute("zongji", a);
                    request.setAttribute("product", product);
                    request.getSession().setAttribute("gouwuche", list);
                    request.getRequestDispatcher("WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request, response);
                }
                else
                {
                    request.getRequestDispatcher("WEB-INF/jsp/test/kucunbuzu.jsp").forward(request, response);
                }

            }

        }
        if (action.equals("selectcar")) {
            String userid = request.getParameter("userid");
            if (userid.length() == 0) {
                request.getRequestDispatcher("/user?flag=").forward(request, response);
            } else {
                List<ShopcartBo> list = sh.selectCar(userid);
                for (ShopcartBo sb1 : list) {
                    a += sb1.getXiaoji();
                }
                request.getSession().setAttribute("zongji", a);
                request.getSession().setAttribute("gouwuche", list);
                request.getRequestDispatcher("WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request, response);
            }
        }
        if (action.equals("deletecar")) {
            String carid = request.getParameter("carid");
            sh.deleteCar(carid);
            List<ShopcartBo> list = sh.selectCar(user.getUserid());
            request.setAttribute("gouwuche", list);
            request.getRequestDispatcher("WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request, response);
        }
        if (action.equals("update")) {
            boolean b = false;
            int a1 = 0;
            int b1 = 0;
            String str = request.getParameter("shoppingCarStr");
            String[] sz = str.split(",");
            for (String zhi : sz) {
                a1++;
                String[] sz1 = zhi.split(":");
                b = sh.selectOldqu(sz1[0], Integer.parseInt(sz1[1]));
                if (b) {
                    b1++;
                } else {
                    b1++;
                }
                if (a1 == b1) {
                    sh.update(sz1[0], user.getUserid(), Integer.parseInt(sz1[1]));
                }
            }
            List<ShopcartBo> list = sh.selectCar(user.getUserid());
            for (ShopcartBo sb1 : list) {
                a += sb1.getXiaoji();
            }
            request.getSession().setAttribute("zongji", a);
            request.getSession().setAttribute("gouwuche", list);
            request.getRequestDispatcher("WEB-INF/jsp/shoppingcart/shoppingcart.jsp").forward(request, response);

        }
        if (action.equals("jiesuan")) {
            int aa = 0;
            List<ShopcartBo> list = sh.selectCar(user.getUserid());
            for (ShopcartBo sb1 : list) {
                aa += sb1.getXiaoji();
            }
            int zj = (Integer) request.getSession().getAttribute("zongji");
            request.getSession().setAttribute("zongji", aa);
            request.getRequestDispatcher("WEB-INF/jsp/shoppingcart/settleaccounts.jsp").forward(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
